|
RAIS
3.2
|
00001 using System; 00002 using System.Collections.Generic; 00003 using System.Text; 00004 00005 namespace RAIS.Common.TableManagement 00006 { 00007 public class FieldData 00008 { 00009 //********************************************************************* 00010 #region Fields 00011 Field field; 00012 string value = string.Empty; 00013 00014 int currentLookupValue = 0; 00015 Dictionary<string, int> currentVisibleLookupValues = new Dictionary<string,int>(); 00016 string currentLookupText = string.Empty; 00017 Dictionary<string, string> currentVisibleLookupTexts = new Dictionary<string, string>(); 00018 00019 Dictionary<int, string> lookupValues = new Dictionary<int, string>(); 00020 Dictionary<int, string> selectedMultiLookupValues = new Dictionary<int, string>(); 00021 Dictionary<int, string> newMultiLookupValues = new Dictionary<int, string>(); 00022 Dictionary<string, string> visibleLookupValues = new Dictionary<string,string>(); 00023 Dictionary<string, string> visibleSelectedMultiLookupValues = new Dictionary<string, string>(); 00024 Dictionary<string, string> savedVisibleLookupValues; 00025 Dictionary<string, string> savedVisibleSelectedMultiLookupValues; 00026 Dictionary<int, string> multiLookupToDelete = new Dictionary<int, string>(); 00027 Dictionary<int, string> multiLookupToInsert = new Dictionary<int, string>(); 00028 bool isUsedAsParameter = false; 00029 FieldData lookupParent; 00030 Dictionary<string, FieldData> lookupChilds = new Dictionary<string, FieldData>(); 00031 string controlName; 00032 bool raisedPostback = false; 00033 //bool inParameters = false; 00034 int nonComplianceMajorCategoryID = 0; 00035 int nonComplianceCategoryID = 0; 00036 #endregion 00037 //********************************************************************* 00038 #region Properties 00039 public Field Field 00040 { 00041 get { return this.field; } 00042 set { this.field = value; } 00043 } 00044 public string Value 00045 { 00046 get { return this.value; } 00047 set { this.value = value; } 00048 } 00049 public int CurrentLookupValue 00050 { 00051 get { return this.currentLookupValue; } 00052 set 00053 { 00054 this.currentLookupValue = value; 00055 this.AddCurrentVisibleLookupValue(this.field.Name, value); 00056 } 00057 } 00058 public string CurrentVisibleLookupValue 00059 { 00060 get 00061 { 00062 string key = this.GetCurrentVisisbleLookupKey(); 00063 return MakeKey(key, this.currentVisibleLookupValues[key]); 00064 } 00065 } 00066 public string CurrentLookupText 00067 { 00068 get { return this.currentLookupText; } 00069 set 00070 { 00071 this.currentLookupText = value; 00072 this.AddCurrentVisibleLookupText(this.field.Name, value); 00073 } 00074 } 00075 public string CurrentVisibleLookupText 00076 { 00077 get 00078 { 00079 string key = this.GetCurrentVisisbleLookupKey(); 00080 return this.currentVisibleLookupTexts[key]; 00081 } 00082 } 00083 /* Use this field to store lookup value whict have beed red from DB 00084 * for SingleLookup and MultiLookup (available values) fields */ 00085 public Dictionary<int, string> LookupValues 00086 { 00087 get { return this.lookupValues; } 00088 set { this.AssignLookupValues(value); } 00089 } 00090 /* Use this field to store lookup value whict have beed red from DB 00091 * for MultiLookup (selected values) fields */ 00092 public Dictionary<int, string> SelectedMultiLookupValues 00093 { 00094 get { return this.selectedMultiLookupValues; } 00095 } 00096 public Dictionary<int, string> NewMultiLookupValues 00097 { 00098 get { return this.newMultiLookupValues; } 00099 set { this.newMultiLookupValues = value; } 00100 } 00101 public string NewMultiLookupValuesInXml 00102 { 00103 get 00104 { 00105 string xmlString = ""; 00106 bool hasChilds = (this.lookupChilds != null && this.lookupChilds.Count != 0); 00107 00108 xmlString = "'<Items>"; 00109 if (this.newMultiLookupValues != null) 00110 foreach (KeyValuePair<int, string> kvp in this.newMultiLookupValues) 00111 xmlString += string.Format("<Item ID=\"{0}\"/>" 00112 , ((hasChilds) ? FieldData.MakeKey(this.Field.Name, kvp.Key) : kvp.Key.ToString())); 00113 00114 if (hasChilds) 00115 { 00116 foreach (KeyValuePair<string, FieldData> kvpFieldData in this.lookupChilds) 00117 { 00118 FieldData childFieldData = kvpFieldData.Value; 00119 foreach (KeyValuePair<int, string> kvp in childFieldData.NewMultiLookupValues) 00120 xmlString += string.Format("<Item ID=\"{0}\"/>" 00121 , FieldData.MakeKey(childFieldData.Field.Name, kvp.Key)); 00122 } 00123 } 00124 00125 xmlString += "</Items>'"; 00126 00127 return xmlString; 00128 } 00129 } 00130 public Dictionary<string, string> VisibleLookupValues 00131 { 00132 get { return this.visibleLookupValues; } 00133 } 00134 public Dictionary<string, string> VisibleSelectedMultiLookupValues 00135 { 00136 get { return this.visibleSelectedMultiLookupValues; } 00137 } 00138 public Dictionary<string, string> SavedVisibleLookupValues 00139 { 00140 get { return this.savedVisibleLookupValues; } 00141 set { this.savedVisibleLookupValues = value; } 00142 } 00143 public Dictionary<string, string> SavedVisibleSelectedMultiLookupValues 00144 { 00145 get { return this.savedVisibleSelectedMultiLookupValues; } 00146 set { this.savedVisibleSelectedMultiLookupValues = value; } 00147 } 00148 public Dictionary<int, string> MultiLookupToDelete 00149 { 00150 get { return this.multiLookupToDelete; } 00151 set { this.multiLookupToDelete = value; } 00152 } 00153 public Dictionary<int, string> MultiLookupToInsert 00154 { 00155 get { return this.multiLookupToInsert; } 00156 set { this.multiLookupToInsert = value; } 00157 } 00158 public bool IsUsedAsParameter 00159 { 00160 get { return this.isUsedAsParameter; } 00161 set { this.isUsedAsParameter = value; } 00162 } 00163 public FieldData LookupParent 00164 { 00165 get { return this.lookupParent; } 00166 set { this.lookupParent = value; } 00167 } 00168 public Dictionary<string, FieldData> LookupChilds 00169 { 00170 get { return this.lookupChilds; } 00171 set { this.lookupChilds = value; } 00172 } 00173 public string ControlName 00174 { 00175 get { return controlName; } 00176 set { controlName = value; } 00177 } 00178 public bool RaisedPostback 00179 { 00180 get { return raisedPostback; } 00181 set { raisedPostback = value; } 00182 } 00183 public int NonComplianceMajorCategoryID 00184 { 00185 get { return this.nonComplianceMajorCategoryID; } 00186 set { this.nonComplianceMajorCategoryID = value; } 00187 } 00188 public int NonComplianceCategoryID 00189 { 00190 get { return this.nonComplianceCategoryID; } 00191 set { this.nonComplianceCategoryID = value; } 00192 } 00193 public bool NonComplianceMultilookup 00194 { 00195 get { return (this.Field.Name == Constants.NON_COMPLIANCE_FIELD_NAME); } 00196 } 00197 #endregion 00198 //********************************************************************* 00199 #region Constructors 00200 public FieldData(Field field 00201 , string value 00202 , int currentLookupValue 00203 , string currentLookupText 00204 , Dictionary<int, string> lookupValues 00205 , Dictionary<int, string> selectedMultiLookupValues 00206 , FieldData parentFieldData) 00207 { 00208 this.lookupParent = parentFieldData; 00209 if (this.lookupParent != null) 00210 this.lookupParent.LookupChilds.Add(field.Name, this); 00211 00212 this.field = field; 00213 this.value = value; 00214 this.CurrentLookupValue = currentLookupValue; 00215 this.CurrentLookupText = currentLookupText; 00216 this.AssignSelectedMultiLookupValues(selectedMultiLookupValues); 00217 00218 } 00219 #endregion 00220 //********************************************************************* 00221 #region Public Methods 00222 public void AssignNewMultiLookupValues(Dictionary<string, string> lookupValues) 00223 { 00224 if (lookupValues != null) 00225 foreach (KeyValuePair<string, string> kvp in lookupValues) 00226 this.AddNewMultiLookupValue(kvp.Key, kvp.Value); 00227 } 00228 00229 public void AddLookupValue(string fieldName, int id, string value) 00230 { 00231 string key = FieldData.MakeKey(fieldName, id); 00232 00233 if (this.lookupValues == null) 00234 this.lookupValues = new Dictionary<int, string>(); 00235 00236 if (this.field.Name == fieldName 00237 && !this.lookupValues.ContainsKey(id)) 00238 { 00239 this.lookupValues.Add(id, value); 00240 } 00241 00242 if (this.visibleLookupValues == null) 00243 this.visibleLookupValues = new Dictionary<string, string>(); 00244 if (!this.visibleLookupValues.ContainsKey(key)) 00245 this.visibleLookupValues.Add(key, value); 00246 00247 if (this.lookupParent != null) 00248 this.lookupParent.AddLookupValue(fieldName, id, value); 00249 } 00250 00251 public void AddSelectedMultiLookupValue(string fieldName, int id, string value) 00252 { 00253 string key = FieldData.MakeKey(fieldName, id); 00254 00255 if (this.selectedMultiLookupValues == null) 00256 this.selectedMultiLookupValues = new Dictionary<int, string>(); 00257 00258 if (this.field.Name == fieldName 00259 && !this.selectedMultiLookupValues.ContainsKey(id)) 00260 { 00261 this.selectedMultiLookupValues.Add(id, value); 00262 } 00263 00264 if (this.visibleSelectedMultiLookupValues == null) 00265 this.visibleSelectedMultiLookupValues = new Dictionary<string, string>(); 00266 if (!this.visibleSelectedMultiLookupValues.ContainsKey(key)) 00267 this.visibleSelectedMultiLookupValues.Add(key, value); 00268 00269 if (this.lookupParent != null) 00270 this.lookupParent.AddSelectedMultiLookupValue(fieldName, id, value); 00271 } 00272 00273 public void AddNewMultiLookupValue(string key, string value) 00274 { 00275 string fieldName; 00276 int id; 00277 00278 FieldData.SplitKey(key, out fieldName, out id); 00279 00280 if (this.field.Name == fieldName) 00281 { 00282 if (this.newMultiLookupValues == null) 00283 this.newMultiLookupValues = new Dictionary<int, string>(); 00284 this.newMultiLookupValues.Add(id, value); 00285 } 00286 else 00287 { 00288 if (this.lookupChilds.ContainsKey(fieldName)) 00289 this.lookupChilds[fieldName].AddNewMultiLookupValue(key, value); 00290 } 00291 } 00292 00293 public void AssignCurrentVisibleLookupValueAndText(string key, string text) 00294 { 00295 string fieldName; 00296 int id; 00297 00298 FieldData.SplitKey(key, out fieldName, out id); 00299 00300 if (this.field.Name == fieldName) 00301 { 00302 this.CurrentLookupValue = id; 00303 this.CurrentLookupText = text; 00304 } 00305 else 00306 { 00307 this.CurrentLookupValue = 0; 00308 this.CurrentLookupText = string.Empty; 00309 } 00310 00311 foreach(KeyValuePair<string, FieldData> kvp in this.lookupChilds) 00312 { 00313 FieldData lookupChild = kvp.Value; 00314 lookupChild.AssignCurrentVisibleLookupValueAndText(key, text); 00315 } 00316 } 00317 00318 static public string MakeKey(string fieldName, int id) 00319 { 00320 return "[" + fieldName + "]" + id.ToString(); 00321 } 00322 #endregion 00323 //********************************************************************* 00324 #region Private Methods 00325 private void AssignLookupValues(Dictionary<int, string> lookupValues) 00326 { 00327 if (this.lookupValues == null) 00328 this.lookupValues = new Dictionary<int, string>(); 00329 else 00330 { 00331 this.lookupValues.Clear(); 00332 this.visibleLookupValues.Clear(); 00333 } 00334 00335 if (lookupValues != null) 00336 foreach (KeyValuePair<int, string> kvp in lookupValues) 00337 this.AddLookupValue(this.field.Name, kvp.Key, kvp.Value); 00338 } 00339 00340 private void AssignSelectedMultiLookupValues(Dictionary<int, string> lookupValues) 00341 { 00342 if (this.selectedMultiLookupValues == null) 00343 this.selectedMultiLookupValues = new Dictionary<int, string>(); 00344 else 00345 this.selectedMultiLookupValues.Clear(); 00346 00347 if (lookupValues != null) 00348 foreach (KeyValuePair<int, string> kvp in lookupValues) 00349 this.AddSelectedMultiLookupValue(this.field.Name, kvp.Key, kvp.Value); 00350 } 00351 00352 private void AddCurrentVisibleLookupValue(string fieldName, int value) 00353 { 00354 if (this.currentVisibleLookupValues.ContainsKey(fieldName)) 00355 this.currentVisibleLookupValues[fieldName] = value; 00356 else 00357 this.currentVisibleLookupValues.Add(fieldName, value); 00358 00359 if (this.lookupParent != null) 00360 this.lookupParent.AddCurrentVisibleLookupValue(fieldName, value); 00361 } 00362 00363 private void AddCurrentVisibleLookupText(string fieldName, string value) 00364 { 00365 if (this.currentVisibleLookupTexts.ContainsKey(fieldName)) 00366 this.currentVisibleLookupTexts[fieldName] = value; 00367 else 00368 this.currentVisibleLookupTexts.Add(fieldName, value); 00369 00370 if (this.lookupParent != null) 00371 this.lookupParent.AddCurrentVisibleLookupText(fieldName, value); 00372 } 00373 00374 private string GetCurrentVisisbleLookupKey() 00375 { 00376 string key = string.Empty; 00377 foreach (KeyValuePair<string, string> kvp in this.currentVisibleLookupTexts) 00378 { 00379 if (kvp.Value != string.Empty) 00380 { 00381 key = kvp.Key; 00382 break; 00383 } 00384 } 00385 00386 return (key == string.Empty) ? this.Field.Name : key; 00387 } 00388 00389 static private void SplitKey(string key, out string fieldName, out int id) 00390 { 00391 int firstPost = key.IndexOf('[') + 1; 00392 int lastPost = key.IndexOf(']') - 1; 00393 if (lastPost < 0) 00394 fieldName = string.Empty; 00395 else 00396 fieldName = key.Substring(firstPost, lastPost - firstPost + 1); 00397 id = int.Parse(key.Substring(lastPost + 2, key.Length - lastPost - 2)); 00398 } 00399 #endregion 00400 } 00401 }